package router

import (
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"log"
	"net"
	login "test.com/project-grpc/user/login"
	"test.com/project-user/config"
	loginServiceV1 "test.com/project-user/pkg/service/login.service.v1"
)

type Router interface {
	Route(r *gin.Engine)
}

// 注册路由结构体
type RegisterRouter struct {
}

func New() *RegisterRouter {
	return &RegisterRouter{}
}
func (*RegisterRouter) Route(ro Router, r *gin.Engine) {
	ro.Route(r)
}

// 接口列表
var routers []Router

func InitRouter(r *gin.Engine) {
	for _, ro := range routers {
		ro.Route(r)
	}
}

func Register(ro ...Router) {
	routers = append(routers, ro...)
}

type gRPCConfig struct {
	Addr         string
	RegisterFunc func(*grpc.Server)
}

func RegisterGrpc() *grpc.Server {

	//创建一个grpc配置对象
	c := gRPCConfig{
		Addr: config.C().GC.Addr,
		RegisterFunc: func(g *grpc.Server) {
			login.RegisterLoginServiceServer(g, loginServiceV1.New())
		}}

	s := grpc.NewServer()
	c.RegisterFunc(s)

	lis, err := net.Listen("tcp", c.Addr)
	if err != nil {
		log.Println("cannot listen")
	}
	go func() {
		err = s.Serve(lis)
		if err != nil {
			log.Println("server started error", err)
			return
		}
	}()
	return s
}

//func RegisterEtcdServer() {
//	etcdRegister := discovery.NewResolver(config.C().EtcdConfig.Addrs, logs.LG)
//	resolver.Register(etcdRegister)
//	info := discovery.Server{
//		Name:    config.C().GC.Name,
//		Addr:    config.C().GC.Addr,
//		Version: config.C().GC.Version,
//		Weight:  config.C().GC.Weight,
//	}
//	r := discovery.NewRegister(config.C().EtcdConfig.Addrs, logs.LG)
//	_, err := r.Register(info, 2)
//	if err != nil {
//		log.Fatalln(err)
//	}
//}
